package com.lyxy.service.imp;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lyxy.anno.MyLog;
import com.lyxy.mapper.EmpMapper;
import com.lyxy.pojo.Emp;
import com.lyxy.pojo.PageBean;
import com.lyxy.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class EmpServiceImp implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    /**
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean page(String name, Short gender, Date begin, Date end, Integer page, Integer pageSize) {
        //封装分页参数
        PageHelper.startPage(page, pageSize);
        //执行查询数据将数据分装
        List<Emp> list = empMapper.page(name, gender, begin, end);
        Page pageList = (Page<Emp>) list;
        //封装pageBean对象
        PageBean pageBean = new PageBean(pageList.getTotal(), pageList.getResult());
        System.out.println(pageList.getResult());
        return pageBean;
    }

    @Transactional(rollbackFor = Exception.class) //创建事务，发生所有异常都回滚
    @MyLog
    @Override
    public void delete(List<Long> ids) {
        empMapper.delete(ids);
    }

    @MyLog
    @Override
    public void add(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    @MyLog
    @Override
    public Emp get(Integer id) {
        return empMapper.select(id);
    }


    @MyLog
    @Transactional(rollbackFor = Exception.class) //创建事务，发生所有异常都回滚
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }


    /**
     *
     * 登录
     * @param emp
     * @return
     */
    @Override
    public Emp login(Emp emp) {
        return empMapper.getByUsernameAndPassword(emp);
    }

//    @Override
//    public PageBean page(Emp emp,Integer page, Integer pageSize) {
//        PageBean pageBean = new PageBean();
//
//        //查询总记录数
//        Long count = empMapper.count(emp);
//        //获取数据列表
//        List<Emp> list = empMapper.page(emp,(page-1)*pageSize,pageSize);
//        pageBean.setTotal(count);
//        pageBean.setRows(list);
//        return pageBean;
//    }
}
